package com.shycloud.mido.member.quartz;

import cn.jpush.api.push.PushResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joy.zebra.common.push.JPushUtils;
import com.shycloud.mido.member.api.entity.SmMemberPush;
import com.shycloud.mido.member.api.entity.SmMemberPushQuartz;
import com.shycloud.mido.member.mapper.SmMemberPushQuartzMapper;
import com.shycloud.mido.member.service.SmMemberPushService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 作业推送定时任务类
 *
 * @author nianhua.jiang
 * @date 2021/1/18 11:24
 **/
@Slf4j
public class PushMessageJob implements Job {

	@Autowired
	private SmMemberPushQuartzMapper pushQuartzMapper;
	@Autowired
	private SmMemberPushService pushMessageService;

	private static Log logger = LogFactory.getLog(PushMessageJob.class);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {


		// 获取待推送的教师信息 （提醒教师查看学生作业）
		List<String> regIds1 = pushQuartzMapper.getPushMember("1");
		// 获取教师点评学生的学生信息
		List<String> regIds2 = pushQuartzMapper.getPushMember("2");
		// 获取教师提醒学生的学生信息
		List<String> regIds3 = pushQuartzMapper.getPushMember("3");

		//获取推送消息
		SmMemberPush message1 = pushMessageService.getById(1);
		SmMemberPush message2 = pushMessageService.getById(2);
		SmMemberPush message3 = pushMessageService.getById(3);

		// 推送内容
		PushResult resultPush1 = JPushUtils.sendJPush(message1.getTitle(), message1.getMessage(), regIds1);

		if (resultPush1.statusCode == 0) {
			pushQuartzMapper.delete(new QueryWrapper<SmMemberPushQuartz>().eq("type", "1"));
		}
		PushResult resultPush2 = JPushUtils.sendJPush(message2.getTitle(), message2.getMessage(), regIds2);
		if (resultPush2.statusCode == 0) {
			pushQuartzMapper.delete(new QueryWrapper<SmMemberPushQuartz>().eq("type", "2"));
		}
		PushResult resultPush3 = JPushUtils.sendJPush(message3.getTitle(), message3.getMessage(), regIds3);
		if (resultPush3.statusCode == 0) {
			pushQuartzMapper.delete(new QueryWrapper<SmMemberPushQuartz>().eq("type", "3"));
		}

	}

}
